//
//  AppDelegate.m
//  uniquelove
//
//  Created by ibokan on 14-12-5.
//  Copyright (c) 2014年 ibokan. All rights reserved.
//

#import "AppDelegate.h"
#import "LoadingViewController.h"
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    [WeiboSDK enableDebugMode:YES];
    [WeiboSDK registerApp:kAppKey];
    
    
    self.stream = [XMPPStream new];
    
   /* 
    //找到存储在NSUserDefaults里面的expirationDate（验证过期时间）
    NSDate *date = [[NSUserDefaults standardUserDefaults]objectForKey:@"expirationDate"];
    
    //找到
    UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    
    //有效期内
    if (date != nil && [date timeIntervalSinceNow] >= 0) {
        
        NSString *str = [[NSUserDefaults standardUserDefaults]objectForKey:@"accessToken"];
        NSLog(@"%@",str);
        
        //拿到identifier为@“nav”的controller
        UINavigationController *nav = [mainStoryBoard instantiateViewControllerWithIdentifier:@"nav"];
        self.window.rootViewController = nav;
    }else{
        //有效期外
        //拿到identifier为@“login”的controller
        LoadingViewController *loginController = (LoadingViewController *)[mainStoryBoard instantiateViewControllerWithIdentifier:@"login"];
        self.window.rootViewController = loginController;
        
        //拿到identifier为@“nav1”的controller
//        UINavigationController *nav1 = [mainStoryBoard instantiateViewControllerWithIdentifier:@"nav1"];
//        self.window.rootViewController = nav1;
    }
    [self.window makeKeyAndVisible];
    */
    
    return YES;
}
							
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}







#pragma mark 这里是代理中request的两个方法
/**
 收到一个来自微博客户端程序的请求
 
 收到微博的请求后，第三方应用应该按照请求类型进行处理，处理完后必须通过 [WeiboSDK sendResponse:] 将结果回传给微博
 @param request 具体的请求对象
 */
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request{
    NSLog(@"log in -------------------");
}

/**
 收到一个来自微博客户端程序的响应
 
 收到微博的响应后，第三方应用可以通过响应类型、响应的数据和 WBBaseResponse.userInfo 中的数据完成自己的功能
 @param response 具体的响应对象
 */
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response{
    
#pragma mark 判断响应类型，如果是我们微博授权的则进入代码
    if ([response isKindOfClass:[WBAuthorizeResponse class]]) {
        WBAuthorizeResponse *authResponse = (WBAuthorizeResponse *)response;
        
        //如果没有进行授权成功，退出
        if (authResponse.accessToken == nil) {
            return;
        }
        
        /**
         用户ID
         */
        NSLog(@"userID = %@",authResponse.userID);
        /**
         认证口令
         */
        NSLog(@"%@",authResponse.accessToken);
        /**
         认证过期时间
         */
        NSLog(@"%@",authResponse.expirationDate);
        
#pragma mark 将accessToken，expirationDate保存到本地
        [[NSUserDefaults standardUserDefaults]setObject:authResponse.userID forKey:@"userID"];
        [[NSUserDefaults standardUserDefaults]setObject:authResponse.accessToken forKey:@"accessToken"];
        [[NSUserDefaults standardUserDefaults]setObject:authResponse.expirationDate forKey:@"expirationDate"];
#pragma mark 让NSUserDefaults同步数据
        [[NSUserDefaults standardUserDefaults]synchronize];
        
        //找到
        UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
        
        UINavigationController *nav = (UINavigationController *)[mainStoryBoard instantiateViewControllerWithIdentifier:@"nav"];
        
//        LoadedWeiBoViewController *loadedweiaiController = (LoadedWeiBoViewController *)[mainStoryBoard instantiateViewControllerWithIdentifier:@"loadedweibo"];
        
        
        self.window.rootViewController = nav;
        
    }
}


#pragma mark 这里是手动添加的两个方法
// 重写 AppDelegate 的 handleOpenURL 和 openURL 方法
// 必须遵循<UIApplicationDelegate, WeiboSDKDelegate>协议
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    
    return  [WeiboSDK handleOpenURL:url delegate:self];
    
}


-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    
    return [WeiboSDK handleOpenURL:url delegate:self];
}


@end
